#ifndef __LIST_H
#define __LIST_H

struct list {
	struct list *prev, *next;
};

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

/**
 * container_of - cast a member of a structure out to the containing structure
 * @ptr:	the pointer to the member.
 * @type:	the type of the container struct this is embedded in.
 * @member:	the name of the member within the struct.
 *
 */
#define container_of(ptr, type, member) ({			\
	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
	(type *)( (char *)__mptr - offsetof(type,member) );})

#define LIST_HEAD_INIT(name) {&(name),&(name)}

#define LIST_HEAD(name) struct list name = \
	LIST_HEAD_INIT(name)

static inline void INIT_LIST_HEAD(struct list* list)
{
	list->next = list->prev = list;
}

/**
 * list_entry - get the struct for this entry
 * @ptr:	the &struct list_head pointer.
 * @type:	the type of the struct this is embedded in.
 * @member:	the name of the list_head within the struct.
 */
#define list_entry(ptr, type, member) \
	container_of(ptr, type, member)

 /**
  * list_for_each_entry	-	iterate over list of given type
  * @pos:	the type * to use as a loop cursor.
  * @head:	the head for your list.
  * @member:	the name of the list_head within the struct.
  */
#define list_for_each_entry(pos, head, member)				\
	for (pos = list_entry((head)->next, typeof(*pos), member);	\
	     &pos->member != (head); 	\
	     pos = list_entry(pos->member.next, typeof(*pos), member))

  /**
   * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
   * @pos:	the type * to use as a loop cursor.
   * @n:		another type * to use as temporary storage
   * @head:	the head for your list.
   * @member:	the name of the list_head within the struct.
   */
#define list_for_each_entry_safe(pos, n, head, member)			\
	for (pos = list_entry((head)->next, typeof(*pos), member),	\
		n = list_entry(pos->member.next, typeof(*pos), member);	\
	     &pos->member != (head);					\
	     pos = n, n = list_entry(n->member.next, typeof(*n), member))

void list_add_head(struct list *_new, struct list *head);
void list_add_tail(struct list *_new, struct list *head);
void list_del(struct list *entry);
int list_is_last(const struct list *entry, const struct list *head);
int list_empty(const struct list *head);

#endif/*__LIST_H*/
